﻿using Quartz;
using System;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow.BusinessImpl;
using Wicresoft.UnifyShow.Utility;

namespace Wicresoft.UnifyShow.PlanServices.Task
{
    public class SMSTask : Basic, IJob
    {

        private IPlanManagement _plan = new PlanManagement();

        private string insertSql = @"
                insert into SMSTask(TaskID,JobNo,JobId,CycleId,TaskName,Status,OwnerID,SMSContent,CreateType,CreateTime,TelNumber,From_User,To_User)
            ";
        public void Execute(IJobExecutionContext context)
        {
            Logger.Log("start SMS Task");

            try
            {
                using (var trans = oda.BeginTransaction())
                {
                    try
                    {
                        //insert
                        var sql = insertSql + selectNewTaskSql;
                        ModelHelper<SMSTaskModel>.CreateModel_By_DirectSql(sql, oda);

                        trans.Commit();
                    }
                    catch (Exception e)
                    {
                        Logger.Error("SMS Task", e);
                        trans.Rollback();
                    }
                }
            }
            finally
            {
                oda.Dispose();
            }
        }

        private string selectNewTaskSql = @"
            select a.id,a.jobno,a.jobid,a.cycleid,a.taskname,0,a.ownerid,'任务提醒：'||a.jobno||',创建人：'|| b.displayname||',摘要：' ||a.taskname,1,sysdate,c.phone,a.CreateUserID,a.ownerid 
            from task a
            left join user_x b on b.id = a.createuserid
            left join accountcopy@selfservice c on b.name = c.accountid
            where a.status = 1 
            and not exists (select * from smstask d where d.createtype=1 and a.id = d.taskid)
            order by a.id
        ";
    }
}
